/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.edu.upc.dew.cobranzas.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import pe.edu.upc.dew.cobranzas.model.Contrato;
import pe.edu.upc.dew.cobranzas.model.Cuenta;
import pe.edu.upc.dew.cobranzas.model.Cuota;
import pe.edu.upc.dew.cobranzas.model.Pago;
import pe.edu.upc.dew.cobranzas.service.ContratoServiceImpl;
import pe.edu.upc.dew.cobranzas.service.CuentaServiceImpl;
import pe.edu.upc.dew.cobranzas.service.CuotaServiceImpl;

/**
 *
 * @author Ivan
 */
public class PagoDaoImpl implements PagoDao {

    public List<Pago> pagos = new ArrayList<Pago>();
    private CuentaServiceImpl cuentaService;
    private ContratoServiceImpl contratoService;
    private CuotaServiceImpl cuotaService;

    public void setCuentaService(CuentaServiceImpl cuentaService) {
        this.cuentaService = cuentaService;
    }

    public void setContratoService(ContratoServiceImpl contratoService) {
        this.contratoService = contratoService;
    }

    public void setCuotaService(CuotaServiceImpl cuotaService) {
        this.cuotaService = cuotaService;
    }

    public int getNuevoCodido() {
        return pagos.size() + 1;
    }

    public List<Pago> Listado() {
        pagos.clear();
        Connection connection = null;
        Pago pago = null;
        Statement st = null;
        ResultSet rs = null;
        try {
            connection = ConnectionUtils.getConnection();
            st = connection.createStatement();
            rs = st.executeQuery("select * from T_Pago");
            rs.next();
            do {

                pago = new Pago();
                pago.setIdPago(rs.getInt("idPago"));
                pago.setFecha(rs.getString("fecha"));
                pago.setMonto(rs.getDouble("monto"));
                pago.setFormaPago(rs.getString("formaPago"));
                pago.setObservaciones(rs.getString("observaciones"));
                pago.setCuenta(cuentaService.getCuentaxNroCuenta(rs.getString("numCuenta")));
                pago.setContrato(contratoService.getContratoPorCodigo(rs.getInt("idContrato")));
                pago.setCuota(cuotaService.getCuota(contratoService.getContratoPorCodigo(rs.getInt("idContrato")), rs.getInt("idCuota")));

                this.pagos.add(pago);

            } while (rs.next());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            ConnectionUtils.closeResultSet(rs);
            ConnectionUtils.closeStatement(st);
            ConnectionUtils.closeConnection(connection);
        }
        return pagos;
    }

     public boolean Registra(int idPago, String fecha, Double monto, String formaPago,String observaciones,Cuenta cuenta, Contrato contrato, Cuota cuota) {
        Connection connection = null;
        Statement st = null;
        try {
            connection = ConnectionUtils.getConnection();
            st = connection.createStatement();
            st.executeQuery("insert into T_Pago values (" + idPago + ",'" + fecha + "'," + monto + ",'" + formaPago + "','" + observaciones + "','" + cuenta.getNumCuenta() + "'," + contrato.getIdContrato() + "," + cuota.getIdCuota() + ")");
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {

            ConnectionUtils.closeStatement(st);
            ConnectionUtils.closeConnection(connection);
            return true;
        }

    }


      public boolean Elimina(int idPago) {
        Connection connection = null;
        Statement st = null;
        try {
            connection = ConnectionUtils.getConnection();
            st = connection.createStatement();
            st.executeQuery("delete from T_Pago where idPago=" + idPago + "");
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {

            ConnectionUtils.closeStatement(st);
            ConnectionUtils.closeConnection(connection);
            return true;
        }

    }
}
